<template>
  <div class="bottom-bar">
    <div class="check-content" @click="checkClick">
      <check-button class="check-button" :is-checked="isSelectAll"></check-button>
      <span>全选</span>
    </div>
    <div class="price">
      合计：{{totalPrice}}
    </div>
    <div class="calculate" @click="calcClick">
      去计算({{checkLength}})
    </div>
  </div>
</template>

<script>
import CheckButton from 'components/content/checkButton/CheckButton'
import {mapGetters} from 'vuex'

export default {
  name:'CartBottomBar',
  components:{
    CheckButton
  },
  computed:{
    ...mapGetters(['cartList']),
    totalPrice(){
      return '￥' + this.cartList.filter(item =>{
        return item.checked
      }).reduce((preValue,item)=>{
         return preValue + item.price * item.count
      },0)
    },
    checkLength(){
      return this.cartList.filter(item => item.checked).length
      
    },
    isSelectAll(){
      if(this.cartList.length === 0) return false
      return !this.cartList.find(item => !item.checked)
    }
  },
  methods:{
    checkClick(){
      if(this.isSelectAll){
        this.cartList.forEach(item => item.checked = false) 
      }else{
        this.cartList.forEach(item => item.checked = true)
      }
    },
    calcClick(){
      if(this.cartList.length === 0){
        this.$toast.show('请先添加商品',2000)
      }else if(!this.isSelectAll){
        this.$toast.show('都得买',2000)
      }else{
        this.$toast.show('👌',2000)
      }
    }
  }
}
</script>

<style scoped>
  .bottom-bar{
    height: 40px;
    background-color: #eee;
    position: relative;
    line-height: 40px;
    display: flex;
    font-size: 14px;
  }
  .check-content{
    display:flex;
    align-items:center;
    justify-content: center;
    width: 20%;
  }
  .check-button{
    width: 20px;
    height: 20px;
    line-height: 20px;
  }
  .price{
    display: flex;
    justify-content: center;
    flex: 1;
  }
  .calculate{
    width: 30%;
    background-color: red;
    color: #fff;
    text-align: center;
  }
  
</style>